﻿using ST.Library.UI.NodeEditor;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace STNode_A2B.STNodeControls
{
    public class STNodeControl_TextBox : STNodeControl
    {
        private Rectangle rectangle;

        private string editText = "";
        private int address;

        private bool _isAddress = false;
        private bool _isIntData = true;

        public bool IsAddress
        {
            get { return _isAddress; }
            set
            {
                _isAddress = value;
            }
        }

        public bool IsIntData
        {
            get { return _isIntData; }
            set
            {
                _isIntData = value;
            }
        }

        public string EditorText
        {
            get { return editText; }
            set
            {
                // 可以在这里添加更多的验证逻辑，以确保设置的文本符合预期的格式要求
                if (int.TryParse(value, out address))
                {
                    if(_isAddress)
                    {
                        editText = "0x" + address.ToString("X");
                    }
                    else
                    {
                        editText = value;
                    }
                    Invalidate(); // 调用 Invalidate 方法来通知控件需要重绘
                }
                else if(!_isIntData)
                {
                    editText = value;
                    Invalidate();
                }
            }
        }

        public STNodeControl_TextBox()
        {
            // 可以在构造函数中初始化一些控件的默认属性或者其他设置
            // 例如，设置控件的大小、颜色等
            this.Width = 120;
            this.Height = 20;
        }

        protected override void OnPaint(DrawingTools dt)
        {
            base.OnPaint(dt); // 如果有需要，可以调用基类的 OnPaint 方法

            Graphics g = dt.Graphics;
            SolidBrush brush = dt.SolidBrush;

            // 绘制边框
            Pen borderPen = new Pen(Color.Black, 1f);
            rectangle = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            g.DrawRectangle(borderPen, rectangle);

            // 绘制背景
            g.FillRectangle(brush, rectangle);

            // 绘制文本
            //brush.Color = Color.Black;
            //g.DrawString(EditorText, this.Font, brush, new PointF(rectangle.Left + 5, rectangle.Top + 2));

            brush.Color = Color.Black;

            StringFormat stringFormat = new StringFormat
            {
                Alignment = StringAlignment.Near,
                LineAlignment = StringAlignment.Near,
                Trimming = StringTrimming.Word,
                FormatFlags = StringFormatFlags.LineLimit
            };

            RectangleF textRectangle = new RectangleF(rectangle.Left + 5, rectangle.Top + 2, rectangle.Width - 10, rectangle.Height - 4);
            g.DrawString(EditorText, this.Font, brush, textRectangle, stringFormat);
        }
    }
}

